
 /**  
 * Project Name:zhonghecrm  
 * File Name:FdfsUtil.java  
 * Package Name:com.crm.util  
 * Date:2016年10月21日下午3:34:02   
 *  
*/  
  
package com.crm.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;

import org.apache.commons.fileupload.FileItem;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.web.multipart.MultipartFile;

/**  
 * ClassName:FdfsUtil <br/>  
 * Function: 文件上传 <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月21日 下午3:34:02 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
public class FdfsUtil {

	private static String configFileName = "/fdfs_client.conf";
	
	private static TrackerClient trackerClient;
	private static TrackerServer trackerServer;
	private static StorageServer storageServer;
	private static StorageClient storageClient;
	private static StorageClient1 storageClient1; 
	
	static{
		
		try {
			
			// 配置文件必须指定全路径
			String confName = FdfsUtil.class.getResource(configFileName).getPath(); 
			// 配置文件全路径中如果有中文，需要进行utf8转码
			confName = URLDecoder.decode(confName, "utf8");
			
			ClientGlobal.init(confName);
			
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageClient = new StorageClient(trackerServer, storageServer);
		} catch (Exception e) {
			throw new RuntimeException("fdfs初始化错误！", e); 
		}

	}
	
    
     /**  
     * uploadFile:(上传文件). <br/>  
     *  
     * @author hanlinxiu  
     * @param file
     * @param uploadFileName
     * @param fileLength
     * @return
     * @throws IOException
     * @throws IOException
     * @throws MyException  
     * @since JDK 1.8  
     */
    public static String[] uploadFile(MultipartFile file, String uploadFileName, long fileLength) throws Exception {  
        
    	byte[] fileBuff = file.getBytes();
    	
        String[] files = null;  
        
        String fileExtName = "";  
        if (uploadFileName.contains(".")) {  
            fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);  
        } else {  
        	throw new RuntimeException("上传文件失败，以为文件名是不合法的！"); 
        }  
  
        // 设置元信息  
        NameValuePair[] metaList = new NameValuePair[3];  
        metaList[0] = new NameValuePair("fileName", uploadFileName);  
        metaList[1] = new NameValuePair("fileExtName", fileExtName);  
        metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));  
  
        // 上传文件  
        files = storageClient.upload_file(fileBuff, fileExtName, metaList);  
        
        trackerServer.close();  
        
        return files;  
    }  
    
    public static String[] uploadFile(MultipartFile file, String fileName, String fileExt, long fileLength) throws Exception {
    	
    	String[] files = null;  
    	
    	byte[] fileBuff = file.getBytes();
    	
    	NameValuePair[] metaList = new NameValuePair[3];  
        metaList[0] = new NameValuePair("fileName", fileName);  
        metaList[1] = new NameValuePair("fileExtName", fileExt);  
        metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));  
        
        files = storageClient.upload_file(fileBuff, fileExt, metaList);  
    	
    	return files; 
    }
    public static String[] uploadFile(byte[] fileContent, String fileName, String fileExt, long fileLength) throws Exception {
    	NameValuePair[] metaList = new NameValuePair[3];  
        metaList[0] = new NameValuePair("fileName", fileName);  
        metaList[1] = new NameValuePair("fileExtName", fileExt);  
        metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));
        String[] result = null;
		synchronized (FdfsUtil.class) {
		  result = storageClient.upload_file(fileContent, fileExt, metaList);
		}
		return result;
	} 
    
     /**  
     * downloadFile:(下载文件). <br/>  
     *  
     * @author hanlinxiu  
     * @param groupName
     * @param filepath
     * @param fileName
     * @throws Exception  
     * @since JDK 1.8  
     */
    public static void downloadFile(String groupName, String filepath, String fileName) throws Exception{  
         
         byte[] b = storageClient.download_file(groupName, filepath);  
         
         //String fileName = "src/main/resources/test1.jpg";    
         FileOutputStream out = new FileOutputStream(fileName);  
         out.write(b);  
         out.flush();  
         out.close();  
    }  
    
    public static void downloadFile1(String filepath, String fileName) throws Exception{  
        
        byte[] b = storageClient1.download_file1(filepath);  
        
        //String fileName = "src/main/resources/test1.jpg";    
        FileOutputStream out = new FileOutputStream(fileName);  
        out.write(b);  
        out.flush();  
        out.close();  
    }  
    
    
     /**  
     * deleteFile:(删除文件). <br/>  
     *  
     * @author hanlinxiu  
     * @param groupName
     * @param filepath
     * @throws Exception  
     * @since JDK 1.8  
     */
    public static void deleteFile(String groupName,String filepath) throws Exception{  
        storageClient.delete_file(groupName, filepath);  
    }  
    
    public static void deleteFile1(String filepath) throws Exception{  
        storageClient1.delete_file1(filepath);  
    } 
	
}
  
